﻿Shader "Unlit/shadow"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}

        _shadowColor("ShadowColor", Color) = (1,0,0,1)  		

        _shadowHeight("ShadowHeight", Range(0,1)) = 0 
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass{
			Name "shadow"
			Tags { "Queue" = "Transparent-100" "RenderType" = "Transparent" }

			LOD 100
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#include "Lighting.cginc"
			#pragma vertex vert
			#pragma fragment frag

			float _shadowHeight;
			float4  _shadowColor;
			struct v2f
			{
				float4 pos : SV_POSITION;
			};

			v2f vert(appdata_base v)
			{
				v2f o;
				float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
				//得到光源方向
				float3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
				//Y轴取反
				float PosY = _shadowHeight - worldPos.y;

				//得到缩放比,简单来说就是光照方向的倾斜度
				float rate = PosY / lightDir.y;
				//利用这个斜度得到X轴和Z轴
				float PosX = lightDir.x* rate + worldPos.x;
				float PosZ = lightDir.z* rate + worldPos.z;
				//得到最终的世界坐标
				float4 FinalworldPos = float4(PosX, _shadowHeight, PosZ, 1.0);
				//注意 因为我们用的是世界坐标了 所以变换矩阵是VP 不是常用的MVP了
				o.pos = mul(UNITY_MATRIX_VP, FinalworldPos);
				return o;
			}

			float4 frag(v2f i) : SV_Target
			{
				//返回这个属性的颜色就行了
				return _shadowColor;
			}
			ENDCG
		}
	}
}
